package niconama04.client;

import niconama04.shared.data.Sentence;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;


public class RegistSentence implements EntryPoint {
	
	/** Create a remote service proxy to talk to the server-side service. */
	final RegistSentenceServiceAsync service = GWT.create(RegistSentenceService.class);

	TextBox textBox;

	Button registButton;

	ResultDialog registDialog;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {

		ViewBuilder.build();

		RootPanel rp = RootPanel.get("gwt_sentence");
		if (rp == null) {
			return;
		}

		registButton = new Button("Regist");
		textBox = new TextBox();
		textBox.setWidth("400px");
		registDialog = new ResultDialog();

		{
			rp.add(textBox);
			rp.add(registButton);
		}

		{
			textBox.setFocus(true);
			textBox.setText("");
		}

		registButton.addClickHandler(new ClickHandler() {

			private AsyncCallback<Sentence> callback = new AsyncCallback<Sentence>(){

				@Override
				public void onFailure(Throwable x_caught) {
					registDialog.show("error.", x_caught.getMessage());
					textBox.setText("");
					textBox.setEnabled(true);
				}

				@Override
				public void onSuccess(Sentence x_result) {
					registDialog.show("success.", x_result.getSentence());
					textBox.setText("");
					textBox.setEnabled(true);
				}
			};

			@Override
			public void onClick(ClickEvent x_event) {
				String value = textBox.getText().trim();
				if (value.equals("")) {
					registDialog.show("empty.", "");
					textBox.setText("");
					textBox.setEnabled(true);
				} else {
					textBox.setEnabled(false);
					service.registServer(value, callback);
				}
			}
		});
	}

	private class ResultDialog extends DialogBox {

		private Label valueLabel;

		private Label resultLabel;

		private Button button;

		ResultDialog() {

			valueLabel = new Label();
			resultLabel = new Label();

			setText("登録結果");
			setAnimationEnabled(true);
			button = new Button("Close");
			VerticalPanel dialogVPanel = new VerticalPanel();
			dialogVPanel.add(valueLabel);
			{
				Label sep = new  Label();
				sep.setHeight("10px");
				dialogVPanel.add(sep);
			}
			dialogVPanel.add(resultLabel);
			dialogVPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			dialogVPanel.add(button);
			setWidget(dialogVPanel);

			button.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					registButton.setEnabled(true);
					textBox.setFocus(true);
					hide();
				}
			});
		}

		void show(String x_value, String x_result) {
			valueLabel.setText(x_value);
			resultLabel.setText(x_result);
			super.show();
			button.setFocus(true);
		}
	}
}
